<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
	die('This page cannot be called directly.') ;
}

class ajax_news {
	function playme(){
        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class
        $this->$code();
        if (method_exists($this, $code)) {
            $this->$code();
        }else{
            $this->home();
        }
	}
	
	function home(){
		global $display;
		die("Nothing to do...");
	}
                 
        function del_news_selected()
        
    {
       
		if(!User::is_login())
		{
			echo "not_login";
			exit;
		}

                if(!User::have_permit("news_del"))
                {
                    echo "no_perm";
                    exit;
                }
         
        
		$news_ids= Url::get("selected_ids");

		if(is_array($news_ids))
		{
            $error = false;
            
            foreach($news_ids as $id)
            {
                if($id == '-1'){
                    $error = TRUE;
                }else{
                        $news 	= DB::select_id('news', $id);
                        $news['status'] = '-1';
                        $news['user_del'] = User::id();
                        $news['user_del_email'] = User::user_email();
                        Db::update_id('news', $news, $id);
                }
            }
            
            if($error)
            {
                echo "un_success";
            }
            else{
                echo "success";
            }
		}
		else
		{
                    
            echo "not_item";
            exit;
        }
	}
        
        function del_tuas_selected()
        
    {
       
		if(!User::is_login())
		{
			echo "not_login";
			exit;
		}

                if(!User::have_permit("news_del"))
                {
                    echo "no_perm";
                    exit;
                }
         
        
		$news_ids= Url::get("selected_ids");

		if(is_array($news_ids))
		{
            $error = false;
            
            foreach($news_ids as $id)
            {
                if($id == '-1'){
                    $error = TRUE;
                }else{
                        $news 	= DB::select_id('news', $id);
                        $news['status'] = '-1';
                        $news['user_del'] = User::id();
                        $news['user_del_email'] = User::user_email();
                        Db::update_id('tourist_attractions', $news, $id);
                }
            }
            
            if($error)
            {
                echo "un_success";
            }
            else{
                echo "success";
            }
		}
		else
		{
                    
            echo "not_item";
            exit;
        }
	}
        function change_status_comment(){
            $id = Url::get('id_comment','');
            $return ='';
            if($id){
                $tmp = DB::select_all('news_comment','id='.$id);
                $com = array();
                foreach ($tmp as $c){
                    if($c['status']==1){
                        $com['status'] = 2;
                        if(DB::update('news_comment', $com, 'id='.$id)){
                            $return = '<input type="hidden" name="change_status_'.$id.'" id="change_status_'.$id.'" value="'.$id.'">
                                        <i>Ẩn</i> [ <a class="change_status" title="change_status_'.$id.'">Hiện</a> ]';
                            echo $return;
                            exit();
                        }
                    }
                    elseif($c['status']==2)
                        {
                        $com['status'] = 1;
                        if(DB::update('news_comment', $com, 'id='.$id)){
                            $return = '<input type="hidden" name="change_status_'.$id.'" id="change_status_'.$id.'" value="'.$id.'">
                                        Hiện [ <a class="change_status" title="change_status_'.$id.'">Ẩn</a> ]';
                            echo $return;
                            exit();
                        }
                    }
                    break;
                }
            }
            echo $return;
            exit();
        }
        function del_comments_selected()
        
    {
       
		if(!User::is_login())
		{
			echo "not_login";
			exit;
		}

                if(!User::have_permit("comment_admin"))
                {
                    echo "no_perm";
                    exit;
                }
         
        
		$news_ids= Url::get("selected_ids");

		if(is_array($news_ids))
		{
            $error = false;
            
            foreach($news_ids as $id)
            {
                if($id == '-1'){
                    $error = TRUE;
                }else{
                        $news 	= DB::select_id('news_comment', $id);
                        $news['status'] = '-1';
                        if(Db::update_id('news_comment', $news, $id))
                        {}else $error = TRUE;
                }
            }
            
            if($error)
            {
                echo "un_success";
            }
            else{
                echo "success";
            }
		}
		else
		{
                    
            echo "not_item";
            exit;
        }
	}
        function change_order_view(){
            if(!User::is_login())
		{
			echo "not_login";
			exit;
		}
            if(!User::have_permit("Categories_edit"))
                {
                    echo "no_perm";
                    exit;
                }
             $id = Url::get('id_cate','');
             $order = Url::get('order','');
             
             $value = array();
             $value['set_default'] = $order;
             $other = array();
             $other['set_default'] = '0';
             if($id != '' and $order != ''){
                 $a = DB::update('categories', $other,'set_default='.$order);// update order other which setdefault = $order to 0
                 $b = DB::update('categories', $value,'id='.$id);// update new order for the id
                 if($a and $b){
                     echo 'success';
                    exit;
                 }else{
                     echo 'database';
                     exit;
                 }
             }
             else {
                 echo 'error';
                 exit;
             }
        }
}
?>